<template>
  <div id="woa-list">
    <div class="woa-list-wrap">
      <div class="woa-item" v-for="item in list" :key="item.appId" @click="handleItemClick(item)">
        <div class="woa-logo">
          <img width="100%" height="100%" :src="item.headImage || '../../../../image/woa/qingke.jpg'">
        </div>

        <div class="woa-name" >{{item.publicAccountNickname}}</div>
      </div>
      <a class="woa-item add" :href="authUrl">
        +
      </a>
    </div>
  </div>
</template>

<script>
import CONSTANT from '../../constant/woa';
import CCONSTANT from '../../constant/common';
// import cookie from '../../lib/cookie';

export default {
  data() {
    return {
    };
  },
  computed: {
    authUrl() {
      const current = window.location.href;
      return `${CCONSTANT.REQUEST_URL}${CONSTANT.WOA_AUTH_URL}?redirectUrl=${encodeURIComponent(current)}`;
    },
    list() {
      return this.$store.state.woa.woaList || [];
    },
  },
  components: {
  },
  beforeDestroy() {
    setTimeout(() => {
      const meta = document.querySelectorAll('[name="referrer"]')[0];
      meta.parentNode.removeChild(meta);
      const metaE = document.createElement('meta');
      metaE.setAttribute('name', 'referrer');
      metaE.setAttribute('content', 'no-referrer');
      document.head.appendChild(metaE);
    }, 0);
  },
  mounted() {
    if (this.$store.state.login.loginObj.role === 'USER') {
      this.$router.push('/');
      return;
    }
    const meta = document.querySelectorAll('[name="referrer"]')[0];
    meta.parentNode.removeChild(meta);
    const metaE = document.createElement('meta');
    metaE.setAttribute('name', 'referrer');
    metaE.setAttribute('content', 'no-referrer-when-downgrade');
    document.head.appendChild(metaE);
    this.getList();
  },
  methods: {
    getList() {
      this.$store.dispatch('getWoaList');
    },
    handleItemClick(item) {
      this.$confirm('确定刷新当前公众号信息吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        this.$store.dispatch('unbindWoa', {
          appId: item.appId,
        }).then(() => {
          this.$message({
            type: 'success',
            message: `刷新公众号[${item.publicAccountNickname}]成功!`,
          });
          // 清空当前选择的公众号绑定
          // const defaultWoa = JSON.parse(cookie(CCONSTANT.COOKIE_USER_WOA_LIST))[0];
          // if (defaultWoa.appId === item.appId) {
          //   this.$store.dispatch('getWoaList').then((res) => {
          //     cookie(CCONSTANT.COOKIE_USER_WOA_LIST, null);
          //     cookie(CCONSTANT.COOKIE_USER_WOA_LIST, JSON.stringify(res));
          //     window.location.reload();
          //   });
          //   return;
          // }
          setTimeout(() => {
            window.location.reload();
          }, 300);
        }).catch(() => {
          this.$message({
            type: 'warning',
            message: '刷新失败!',
          });
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消',
        });
      });
    },
    add() {
      this.$message({
        type: 'success',
        message: `前往绑定页面! ${CONSTANT.WOA_AUTH_URL}`,
      });
      const current = window.location.href;
      window.location.href = `${CCONSTANT.REQUEST_URL}${CONSTANT.WOA_AUTH_URL}?redirectUrl=${encodeURIComponent(current)}`;
    },
  },
};
</script>

<style lang="less">
#woa-list {
  font-family: Helvetica, sans-serif;
  text-align: center;
  width: 100%;
  .woa-list-wrap {
    display: flex;
    flex-wrap: wrap;
    .woa-item {
      width: 120px;
      height: 150px;
      margin: 10px;
      border: 1px #eee solid;
      &:hover {
        background: #eee;
        cursor: pointer;
      }
      .woa-logo {
        width: 100px;
        height: 100px;
        margin: 10px;
      }
      .woa-name {
      }
    }

    a,
    a:hover,
    a:focus {
      color: #333;
      text-decoration: none;
    }
    .add {
      font-size: 50px;
      line-height: 120px;
    }
  }
}
</style>
